package com.rc.saas.mini.hlhtcec2.service;

import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.hlhtcec2.printer.HlhtPrinterAssemble;
import com.rc.saas.mini.model.hlhtsk.HlhtskBizOperator;
import com.rc.saas.mini.model.hlhtsk.HlhtskBizTenant;
import com.rc.saas.mini.service.hlhtsk.HlhtskBizOperatorService;
import com.rc.saas.mini.service.hlhtsk.HlhtskBizTenantService;
import com.rc.saas.mini.vo.hlhtcec2.TokenResult;
import com.rc.saas.mini.vo.printer.QueryPrintReq;
import com.rc.saas.mini.vo.printer.QueryPrintResult;
import com.rc.saas.mini.vo.printer.StartPrintReq;
import com.rc.saas.mini.vo.printer.StartPrintResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangzhengwei
 * @create 2021/4/23
 */
@Service
public class HlhtPrinterServiceImpl implements HlhtPrinterService {
    private static Logger logger = LogManager.getLogger(HlhtPrinterServiceImpl.class);

    @Autowired
    private HlhtPrinterAssemble hlhtPrinterAssemble;

    @Autowired
    private HlhtBiz2NotifyService hlhtBiz2NotifyService;

    @Autowired
    private HlhtskBizTenantService hlhtskBizTenantService;

    @Autowired
    private HlhtskBizOperatorService hlhtskBizOperatorService;

    @Override
    public TResponse assembleQueryPrintResult(QueryPrintReq queryPrintReq, String tenantCode, String hlhtOperatorId) {
        //查找运营商信息
        HlhtskBizTenant bizTenant = hlhtskBizTenantService.selectByTenantCodeAndOperatorId(tenantCode, hlhtOperatorId, "1");
        //查找运营商信息,申请的互联互通类型,以tenant_code为基准，0反向对接 1正向对接, 默认0
        HlhtskBizOperator bizOperator = hlhtskBizOperatorService.selectByTenantCodeAndOperatorId(tenantCode, hlhtOperatorId, "1");

        //第1步：取Token
        TokenResult tokenResult = hlhtBiz2NotifyService.assembleTokenResult(bizOperator, bizTenant);
        if (tokenResult == null || tokenResult.getSuccStat() != 0) {
            logger.error(bizOperator.getTenantCode() + "-" + bizOperator.getHlhtOperatorId() + ":无法获取Token:{},:{}", queryPrintReq.getStartChargeSeq(), queryPrintReq.getConnectorID());
            return TResponse.FAIL("无法打印机信息,获取不到Token");
        }

        //第2步：下达指令
        QueryPrintResult result = hlhtPrinterAssemble.assembleNotifyQueryPrintResult(queryPrintReq, tokenResult, bizOperator, bizTenant);
        if (result != null && result.getSuccStat() == 0) {
            logger.info(bizTenant.getTenantCode() + "-" + bizTenant.getHlhtOperatorId() + ":查询打印机信息成功:{},:{}", queryPrintReq.getStartChargeSeq(), queryPrintReq.getConnectorID());

            return TResponse.SUCCESS_DATA(result);
        } else {
            logger.info(bizTenant.getTenantCode() + "-" + bizTenant.getHlhtOperatorId() + ":查询打印机信息异常:{},:{}", queryPrintReq.getStartChargeSeq(), queryPrintReq.getConnectorID());
        }

        return null;
    }

    @Override
    public TResponse assembleStartPrintResult(StartPrintReq startPrintReq, String tenantCode, String hlhtOperatorId) {
        //查找运营商信息
        HlhtskBizTenant bizTenant = hlhtskBizTenantService.selectByTenantCodeAndOperatorId(tenantCode, hlhtOperatorId, "1");
        //查找运营商信息,申请的互联互通类型,以tenant_code为基准，0反向对接 1正向对接, 默认0
        HlhtskBizOperator bizOperator = hlhtskBizOperatorService.selectByTenantCodeAndOperatorId(tenantCode, hlhtOperatorId, "1");

        //第1步：取Token
        TokenResult tokenResult = hlhtBiz2NotifyService.assembleTokenResult(bizOperator, bizTenant);
        if (tokenResult == null || tokenResult.getSuccStat() != 0) {
            logger.error(bizOperator.getTenantCode() + "-" + bizOperator.getHlhtOperatorId() + ":无法获取Token:{},:{}", startPrintReq.getStartChargeSeq(), startPrintReq.getConnectorID());
            return TResponse.FAIL("无法打印,获取不到Token");
        }

        //第2步：下达指令
        StartPrintResult result = hlhtPrinterAssemble.assembleNotifyQueryStartPrintResult(startPrintReq, tokenResult, bizOperator, bizTenant);
        if (result != null && result.getSuccStat() == 0) {
            logger.info(bizTenant.getTenantCode() + "-" + bizTenant.getHlhtOperatorId() + ":打印机成功:{},:{}", startPrintReq.getStartChargeSeq(), startPrintReq.getConnectorID());

            return TResponse.SUCCESS_DATA(result);
        } else {
            logger.info(bizTenant.getTenantCode() + "-" + bizTenant.getHlhtOperatorId() + ":打印异常:{},:{}", startPrintReq.getStartChargeSeq(), startPrintReq.getConnectorID());
        }

        return null;
    }

}
